package com.dts.backproject.controller;

import com.dts.backproject.entity.po.Result;
import com.dts.backproject.entity.po.Video;
import com.dts.backproject.exception.MyException;
import com.dts.backproject.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.List;

@RestController
@RequestMapping({"/videos", "/"})
@CrossOrigin(origins = "*")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 获取所有视频
     * 请求方式: GET
     * URL: /api/videos/all
     */
    @GetMapping("/all")
    public List<Video> findAll(){
        return videoService.findAll();
    }


    /**
     * 根据ID获取视频详情
     * 请求方式: GET
     * URL: /api/videos/{id}
     */
    @GetMapping("/{id}")
    public Result<Video> getVideoById(@PathVariable String id) {
        try {
            Video video = videoService.getVideoById(id);
            if (video == null) {
                return Result.error("视频不存在");
            }
            return Result.success(video);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取视频详情失败: " + e.getMessage());
        }
    }

    /**
     * 添加视频
     * 请求方式: POST
     * URL: /api/videos
     */
    @PostMapping
    public Result<String> addVideo(@RequestBody Video video) {
        try {
            videoService.addVideo(video);
            return Result.success("视频添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("视频添加失败: " + e.getMessage());
        }
    }

    /**
     * 更新视频
     * 请求方式: PUT
     * URL: /api/videos
     */
    @PutMapping
    public Result<String> updateVideo(@RequestBody Video video) {
        try {
            videoService.updateVideo(video);
            return Result.success("视频更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("视频更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除视频
     * 请求方式: DELETE
     * URL: /api/videos/{id}
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteVideo(@PathVariable String id) {
        try {
            videoService.deleteVideo(id);
            return Result.success("视频删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("视频删除失败: " + e.getMessage());
        }
    }

    /**
     * 根据上传者邮箱获取视频列表
     * 请求方式: GET
     * URL: /api/videos/uploader/{email}
     */
    @GetMapping("/uploader/{email}")
    public Result<List<Video>> getVideosByUploader(@PathVariable String email) {
        try {
            List<Video> videos = videoService.getVideosByUploader(email);
            return Result.success(videos);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取用户视频列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取视频上传页面
     * 请求方式: GET
     * URL: /api/videos/upload-page
     */
    @GetMapping("/upload-page")
    public String getUploadPage() {
        return "redirect:/video-upload.html";
    }

    /**
     * 获取所有视频 (兼容前端接口)
     * 请求方式: GET
     * URL: /api/AllVideo
     */
    @GetMapping("/AllVideo")
    public List<Video> getAllVideos(@RequestParam(value = "email", required = false) String email) {
        if (email != null && !email.isEmpty()) {
            return videoService.getVideosByUploader(email);
        }
        return videoService.findAll();
    }

    /**
     * 获取视频详情 (兼容前端接口)
     * 请求方式: GET
     * URL: /api/GetVideo/{id}
     */
    @GetMapping("/GetVideo/{id}")
    public Video getVideo(@PathVariable String id) {
        return videoService.getVideoById(id);
    }

    /**
     * 获取视频播放URL (兼容前端接口)
     * 请求方式: GET
     * URL: /api/GetVideoUrl/{id}
     */
    @GetMapping("/GetVideoUrl/{id}")
    public Result<String> getVideoUrl(@PathVariable String id) {
        try {
            Video video = videoService.getVideoById(id);
            if (video == null) {
                return Result.error("视频不存在");
            }
            return Result.success(video.getVideoUrl());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取视频URL失败: " + e.getMessage());
        }
    }
}

